package parsers;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class ParserPruebaSpringerLink {

	/**
	 * 
	 * http://link.springer.com/search/csv?query=software+agile+test
	 * http://link.
	 * springer.com/search/csv?facet-discipline=%22Computer+Science%22
	 * &facet-discipline=%
	 * 22Engineering%22&query=software+agile+test&facet-content-type=%22Article%
	 * 2 2
	 * 
	 * link de acceso
	 * http://link.springer.com/chapter/10.1007/978-1-4614-6663-5_7
	 * 
	 * Para bajar una citacion
	 * http://link.springer.com/export-citation/chapter/10.1007
	 * /978-1-4614-6663-5_7.bib
	 * 
	 * */
	static String[] consultas = { "Software+agile+test",
			"Software+develop+agile+test", "Software+agile+unit+test",
			"Software+develop+agile+unit+test",
			"Software+develop+agile+quality+test",
			"Software+develop+agile+quality+unit+test" };

	public static void main(String[] args) {
		String bibGeneral = "";
		for (int consulta = 0; consulta < consultas.length; consulta++) {
			System.out.println("[" + (consulta+1) + "]" + "iniciando consulta de :"
					+ consultas[consulta].replace('+', ' ') +" / "+ (consultas.length-1));
			String consultaFiltro = "http://link.springer.com/search/csv?date-facet-mode=between&facet-end-year=2015&query="
					+ consultas[consulta]
					+ "&facet-language=%22En%22&showAll=true&facet-discipline=%22Computer+Science%22&facet-content-type=%22Article%22&facet-start-year=2004";
			try {
				String[] lineasHTML = null;
				lineasHTML = cargarURLAsLines(consultaFiltro);
				System.out.println("----------------- Inicio");
				bibGeneral += convertoToBib(lineasHTML);
				System.out.println("----------------- Fin");
			} catch (IOException e2) {
				System.out.println(e2);
				e2.printStackTrace();
			}
		}
		/** Guardar el archivo */
		try {
			guardarArchivo(bibGeneral);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static String[] cargarURLAsLines(String dir) throws IOException {
		URL url = new URL(dir);
		URLConnection spoof;
		spoof = url.openConnection();
		// Spoof the connection so we look like a web browser
		spoof.setRequestProperty("User-Agent",
				"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0;    H010818)");
		BufferedReader in;
		in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
		String strLine = "";

		List<String> lineas = new ArrayList<String>();
		while ((strLine = in.readLine()) != null) {
			// finalHTML += strLine;
			lineas.add(strLine);
		}
		String[] finalHTML = new String[lineas.size()];
		in.close();
		lineas.toArray(finalHTML);
		return finalHTML;
	}

	public static String convertoToBib(String[] lineas) throws IOException {
		int cantidadDeErrores = 0;
		int cantidadDeAciertos = 0;
		String cvsSplitBy = "\",\"";
		String bibGlobal = "";
		for (int i = 1; i < lineas.length; i++) {
			lineas[i] = lineas[i].trim();
			boolean noConforme = false;
			int acumulado = 0;
			if (!lineas[i].endsWith("\"")) {
				for (int j = i + 1; j < lineas.length; j++) {
					if (!lineas[j].endsWith("\"")) {
						lineas[i] += lineas[j];
						acumulado++;
					} else {
						lineas[i] += lineas[j];
						noConforme = true;
						break;
					}
				}
			}
			lineas[i] = lineas[i].replace('\n', ' ');
			lineas[i] = lineas[i].replace('\t', ' ');
			lineas[i] = lineas[i].replace("  ", "");
			String[] parts = lineas[i].split(cvsSplitBy);
			if (noConforme) {
				i += acumulado;
			}
			/**
			 * [0]Item Title, [1]Publication Title, [2]Book Series Title,
			 * [3]Journal Volume, [4]Journal Issue, [5]Item DOI, [6]Authors,
			 * [7]Publication Year, [8]URL, [9]Content Type
			 */
			try {
				// System.out.println(bibGlobal += crearBib(parts)); // debug
				bibGlobal += crearBib(parts);
				/*
				 * System.out.println(i +
				 * " ************* nuevo item ************** \n");
				 * System.out.println("item title: " + parts[0].substring(1));
				 * System.out.println("publication title: " + parts[1]);
				 * System.out.println("book series: " + parts[2]);
				 * System.out.println("Journal volume: " + parts[3]);
				 * System.out.println("Journal issue: " + parts[4]);
				 * System.out.println("Item DOI: " + parts[5]);
				 * System.out.println("Authors: " + parts[6]);
				 * System.out.println("Year: " + parts[7]);
				 * System.out.println("URL: " + parts[8]);
				 * System.out.println("Type: " + parts[9].substring(0,
				 * parts[9].length() - 1));
				 */
				// System.out.println("------------------------------------\n");
				System.out.print(i + " ");
				cantidadDeAciertos++;
			} catch (Exception e) {
				System.err.print(i + " ");
				// System.err.println(e);
				// System.err.println("se presento un problema en el item: " +
				// i);
				// /e.printStackTrace();
				cantidadDeErrores++;
				// break; // debug
			}
		}// for;
		System.out.println("\nCorrectos: " + cantidadDeAciertos);
		System.out.println("Fallidos: " + cantidadDeErrores);
		// 20:45
		return bibGlobal;
	} // metodo convertir

	private static String crearBib(String[] parts) throws IOException {
		/**
		 * [0]Item Title, [1]Publication Title, [2]Book Series Title, [3]Journal
		 * Volume, [4]Journal Issue, [5]Item DOI, [6]Authors, [7]Publication
		 * Year, [8]URL, [9]Content Type *
		 * */
		String bib = "";
		String tag = "" + parts[8];
		/**
		 * procesamiento del resumen a partir de la direccion url obtenida
		 * */
		String resumen = "";
		String baseResumen = cargarURL(parts[8]);
		String[] secciones = baseResumen
				.split("<div class=\"abstract-content formatted\" itemprop=\"description\">");
		resumen = secciones[1].substring(0, secciones[1].indexOf("</div>"));
		resumen = resumen.substring(resumen.indexOf(">") + 1,
				resumen.lastIndexOf("<"));
		resumen = resumen.replace("<em class=\"a-plus-plus\">", "");
		resumen = resumen.replace("</em>", "");
		resumen = resumen.replace("\t", "");
		resumen = resumen.replace("\n", " ");
		try {
			bib += "\n@" + parts[9].substring(0, parts[9].length() - 1) + "{"
					+ tag + ",";
			bib += "author = {" + parts[6] + "},";
			bib += "title = {{" + parts[0].substring(1) + "}},";
			bib += "venue = {" + parts[1] + "},";
			bib += "url = {" + parts[8] + "},";
			bib += "abstract = {" + resumen + "},";
			bib += "doi = {" + parts[5] + "},";
			bib += "year = {" + parts[7] + "}}";
		} catch (Exception e) {
			System.err.println("error al convertir a bib: " + e);
		}
		return bib;
	} // metodo

	public static void guardarArchivo(String bib) throws IOException {
		File f = new File("salidaLibreriaSpringerLink.bib");
		FileWriter salida = new FileWriter(f);
		salida.write(bib);
		salida.flush();
		salida.close();
		System.out.println("Archivo Guardado -----------------");
	}

	public static String cargarURL(String dir) throws IOException {
		String strLine = "";
		String finalHTML = "";
		try {
			URL url = new URL(dir);
			URLConnection spoof;
			spoof = url.openConnection();
			// Spoof the connection so we look like a web browser
			spoof.setRequestProperty("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0;    H010818)");
			BufferedReader in;
			in = new BufferedReader(new InputStreamReader(
					spoof.getInputStream()));

			while ((strLine = in.readLine()) != null) {
				finalHTML += strLine;
			}
			in.close();
		} catch (Exception e) {
			System.err.println("\nAl cargar: " + e);
			return null;
		}
		// System.out.println("saliendo....");
		return finalHTML;
	}

} // clase
